//
//  HNSH_SignViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/4/22.
//

#import "HNSH_SignViewController.h"

#import "HNSH_SignIntegralRecordViewController.h"
#import "HNSH_SignGoodsExChangeRecordViewController.h"
#import "HNSH_GoodsDetailViewController.h"

//view
#import "HNSH_SignHeader.h"
#import "HNSH_SignFooter.h"
#import "HNSH_SignGoodsHeader.h"
#import "HNSH_SignADCell.h"
#import "HNSH_SignGoodsCell.h"

//#import "HNSH_RewardToors.h"

@interface HNSH_SignViewController ()
/* dataArray */
@property (nonatomic, strong) NSMutableArray *dataArray;

/* signModel */
@property (nonatomic, strong) HNSH_SignModel *signModel;

/* showAllDay */
@property (nonatomic, assign) BOOL showAllDay;

/* 签到 */
@property (nonatomic, strong) UILabel *qdLabel;
/* 汇豆 */
@property (nonatomic, strong) UILabel *hdLabel;

@end

static NSString *HNSH_SignHeaderID = @"HNSH_SignHeaderID";
static NSString *HNSH_SignFooterID = @"HNSH_SignFooterID";
static NSString *HNSH_SignGoodsHeaderID = @"HNSH_SignGoodsHeaderID";
static NSString *HNSH_SignADCellID = @"HNSH_SignADCellID";
static NSString *HNSH_SignGoodsCellID = @"HNSH_SignGoodsCellID";

@implementation HNSH_SignViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor appBGColor];
    self.showAllDay = NO;
    
    [self configTopView];
    
    [self configNavView];
    
    [self configCollectionView];
    
    [self loadSignHomeData];
    
    //添加刷新
    [self setupRefresh:self.collectionView option:ST_RefreshDefault];
    //添加空页面
    [self setupEmpty:self.collectionView image:[UIImage imageNamed:@""] title:@""];
    
}

- (void)configNavView {
    [self showNavTitle:@"签到" showLeftItem:YES];
    self.navView.backgroundColor = [UIColor clearColor];
    
    //汇豆兑换
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(kScreenWidth-20-81, kStatusBarHeight+8, 81, 28);
    [button setImage:[UIImage imageNamed:@"mine_sign_top_right_button"] forState:UIControlStateNormal];
    [self.navView addSubview:button];
    
    __weak __typeof(&*self)weakSelf = self;
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        HNSH_SignIntegralRecordViewController *vc = [[HNSH_SignIntegralRecordViewController alloc]init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
}

- (void)configTopView {
    UIImageView *bgImgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 400)];
    bgImgV.image = [UIImage imageNamed:@"mine_sign_top_bg"];
    [self.view addSubview:bgImgV];
    
    UIView *hdBgView = [[UIView alloc]init];
    hdBgView.backgroundColor = [UIColor colorWithHexString:@"#2692F2"];
    [self.view addSubview:hdBgView];
    hdBgView.layer.cornerRadius = 15;
    hdBgView.layer.masksToBounds = YES;
    
    //汇豆
    UILabel *hdLabel = [[UILabel alloc]init];
    hdLabel.textColor = [UIColor whiteColor];
    hdLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:hdLabel];
    self.hdLabel = hdLabel;
    hdLabel.text = @"0汇豆";
    
    [hdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(60);
        make.top.mas_equalTo(kNavBarAndStatusBarHeight+14);
        make.height.mas_equalTo(22);
    }];
    
    
    [hdBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(35);
        make.centerY.equalTo(hdLabel.mas_centerY);
        make.height.mas_equalTo(30);
        make.right.equalTo(hdLabel.mas_right).offset(14);
    }];
    
    UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight+6, 69, 46)];
    icon.image = [UIImage imageNamed:@"mine_sign_hd_icon"];
    [self.view addSubview:icon];
}

- (void)configCollectionView {
    [self.view bringSubviewToFront:self.collectionView];
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(kNavBarAndStatusBarHeight+64, 0, 0, 0));
    }];
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.collectionView registerClass:[HNSH_SignADCell class] forCellWithReuseIdentifier:HNSH_SignADCellID];
    [self.collectionView registerClass:[HNSH_SignGoodsCell class] forCellWithReuseIdentifier:HNSH_SignGoodsCellID];
    
    [self.collectionView registerClass:[HNSH_SignHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HNSH_SignHeaderID];
    [self.collectionView registerClass:[HNSH_SignFooter class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:HNSH_SignFooterID];
    [self.collectionView registerClass:[HNSH_SignGoodsHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HNSH_SignGoodsHeaderID];
}

#pragma mark - UICollectionViewDelegate Methods
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return self.signModel.advert_list.count;
    }
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        HNSH_SignADCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:HNSH_SignADCellID forIndexPath:indexPath];
        cell.model = self.signModel.advert_list[indexPath.row];
        return cell;
    } else {
        HNSH_SignGoodsCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:HNSH_SignGoodsCellID forIndexPath:indexPath];
        cell.model = self.dataArray[indexPath.row];
        return cell;
    }
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return CGSizeMake(kScreenWidth, 54);
    }
    CGFloat w = (kScreenWidth - 50) / 2.0;
    CGFloat h = w + 93;
    return CGSizeMake(w, h);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (section == 0) {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return UIEdgeInsetsMake(10, 20, 10, 20);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return 10;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        if (indexPath.section == 0) {
            HNSH_SignHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HNSH_SignHeaderID forIndexPath:indexPath];
            header.signModel = self.signModel;
            __weak __typeof(&*self)weakSelf = self;
            header.headerBlock = ^(BOOL showAllDay) {
                weakSelf.showAllDay = showAllDay;
                [weakSelf.collectionView reloadData];
            };
            return header;
        }
        HNSH_SignGoodsHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HNSH_SignGoodsHeaderID forIndexPath:indexPath];
        __weak __typeof(&*self)weakSelf = self;
        header.block = ^{
            HNSH_SignGoodsExChangeRecordViewController *vc = [[HNSH_SignGoodsExChangeRecordViewController alloc]init];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        return header;
    } else {
        HNSH_SignFooter *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:HNSH_SignFooterID forIndexPath:indexPath];
        return footer;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        if (self.showAllDay) {
            return CGSizeMake(kScreenWidth, 525);
        }
        return CGSizeMake(kScreenWidth, 192);
    } else {
        return CGSizeMake(kScreenWidth, 32);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(kScreenWidth, 25);
    }
    return CGSizeZero;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        HNSH_AdvertListItem *model = self.signModel.advert_list[indexPath.row];
        if (model.daily_count <= model.complete_count) {
            [self.view makeToast:@"任务已完成"];
            return;
        }
        __weak __typeof(&*self)weakSelf = self;
//        [[HNSH_RewardToors share] showADWithController:weakSelf complate:^(BOOL success) {
//            if (success) {
//                [weakSelf loadSignAdvertProgressWithKey:model.key];
//            }
//        }];
    } else {
        HNSH_GoodsListModel * model = self.dataArray[indexPath.row];
        HNSH_GoodsDetailViewController * vc = [HNSH_GoodsDetailViewController new];
        vc.goodsId = model.uid;
        [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
    }
    
}

#pragma mark networking
- (void)loadSignHomeData {
    [HNSH_UserNetServer requestSignHomeDataSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            self.signModel = [HNSH_SignModel yy_modelWithDictionary:responseObject.resultset];

            self.hdLabel.text = [NSString stringWithFormat:@"%@汇豆",self.signModel.bean_balance];

            [self.collectionView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)refreshData:(NSInteger)page {
    [HNSH_ShopNetServer requestGoodsListWithShop_id:@"" mcate_id:@"" scate_id:@"" keyword:@"" sort_type:@"" sort_rule:@"" type:@"20" page_num:page success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            if (page == 1) {
                [self.dataArray removeAllObjects];
            }
            NSArray *ary = [NSArray yy_modelArrayWithClass:[HNSH_GoodsListModel class] json:responseObject.resultset[@"list"]];
            [self.dataArray addObjectsFromArray:ary];
            [self.collectionView reloadData];
            [self endRefresh:ary.count >= 10];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)loadSignAdvertProgressWithKey:(NSString *)key {
    [HNSH_UserNetServer requestSignAdvertProgressWithKey:key success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [self loadSignHomeData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}


#pragma mark lazy
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [@[] mutableCopy];
    }
    return _dataArray;
}


@end
